package com.huangpuweb.api.service.imp;

import com.huangpuweb.api.model.*;
import com.huangpuweb.api.service.XuJianApiService;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@Service
public class XuJianServiceImp extends BaseXuJianServiceImp implements XuJianApiService {


    @Override
    public Integer getListSysHpEmergencyEventCountByYear(Date baseYear) {
        String sql="select count(id) from V_EVENT where event_time >= ?";
        return queryInteger(sql,new Object[]{baseYear});
    }

    @Override
    public Integer getListHasSysHpEmergencyEventCountByYear(Date baseYear) {
        String sql="select count(id) from V_EVENT where event_time >= ? and event_status=501";
        return queryInteger(sql,new Object[]{baseYear});
    }

    @Override
    public List<XJEvent> getListSysHpEmergencyEvent() {
        //and id <> 811
        String sql="select * from V_EVENT where event_gis <>'' and event_gis is not null and id <> 811 order by event_time desc limit 10 ";
        return getEntryList(sql,new XJEventRowMapper(),new Object[]{});
    }

    @Override
    public String getEventCateNameById(int event_cate) {
        String sql="select tepe_name from T_EVENT_TYPE where id=?";
        return queryString(sql,new Object[]{event_cate});
    }

    @Override
    public List<SysHpXiaoFangAjType> getListWuziByEnercy() {
        String sql="select c.cate as name,sum(n.supplies_num) as count from T_EMER_WARE_HOUSE_LINK_SUPPLIES n join T_EMER_SUPPLIES c on c.id = n.id group by c.cate order by supplies_num desc";
        return getEntryList(sql,new SysHpXiaoFangAjTypeRowMapper(),new Object[]{});
    }

    @Override
    public Long getTeamGroupCount() {
        String sql="select count(1) from T_RESCUE_TEAM ";
        return queryLong(sql,new Object[]{});
    }

    @Override
    public List<SysHpXiaoFangAjType> getListEmergencyType(Date baseYear) {
        //and id <> 811
        String sql="select type_name as name,event_level as count,kind_name from V_EVENT where event_time >= ? and event_gis <>'' and event_gis is not null and id <> 811 ";
        return getEntryList(sql, new RowMapper<SysHpXiaoFangAjType>() {
            @Override
            public SysHpXiaoFangAjType mapRow(ResultSet rs, int rowNum) throws SQLException {
                SysHpXiaoFangAjType  lj= new SysHpXiaoFangAjType();
                lj.setName(rs.getString("name"));
                lj.setCount(rs.getString("count"));
                lj.setKind_name(rs.getString("kind_name"));

                return lj;
            }
        }, new Object[]{baseYear});
    }

    @Override
    public XJEvent getXjEventByEventId(String eventid) {
        String sql="select * from V_EVENT where id=? and event_gis <>'' and event_gis is not null and  id <> 811 ";
        return getEntry(sql,new XJEventRowMapper(),new Object[]{eventid});
    }

    @Override
    public List<XJTShelter> getListXJTShelter() {
        String sql="select * from T_SHELTER where is_delete=0";
        return getEntryList(sql, new RowMapper<XJTShelter>() {
            @Override
            public XJTShelter mapRow(ResultSet rs, int i) throws SQLException {
                XJTShelter lj=new XJTShelter();
                lj.setShelter_lon(rs.getString("shelter_lon"));
                lj.setShelter_lat(rs.getString("shelter_lat"));
                return lj;
            }
        }, new Object[]{});
    }

    @Override
    public List<XJTRescueTeam> getListXJTRescueTeam() {
        String sql="select * from T_RESCUE_TEAM where is_delete=0";
        return getEntryList(sql,new XJTRescueTeamRowMapper(),new Object[]{});
    }

    @Override
    public List<XJTEmerPonding> getListXJTEmerPonding() {
        String sql="select * from T_EMER_PONDING where is_delete=0";
        return getEntryList(sql,new XJTEmerPondingRowMapper(),new Object[]{});
    }

    @Override
    public List<XJTEmerGasStation> getListXJTEmerGasStation() {
        String sql="select * from T_EMER_GAS_STATION where is_delete=0";
        return getEntryList(sql,new XJTEmerGasStationRowMapper(),new Object[]{});
    }

    @Override
    public List<XJTTransportCompany> getListXJTTransportCompany() {
        String sql="select * from T_TRANSPORT_COMPANY where is_delete=0";
        return getEntryList(sql,new XJTTransportCompanyRowMapper(),new Object[]{});
    }

    @Override
    public List<XJTEmerSubwayStation> getListXJTEmerSubwayStation() {
        String sql="select * from T_EMER_SUBWAY_STATION where is_delete=0";
        return getEntryList(sql,new XJTEmerSubwayStationRowMapper(),new Object[]{});
    }

    @Override
    public List<XJTEmerTunnel> getListXJTEmerTunnel() {
        String sql="select * from T_EMER_TUNNEL where is_delete=0";
        return getEntryList(sql,new XJTEmerTunnelRowMapper(),new Object[]{});
    }

    @Override
    public List<XJTEmerUndergroundPassage> getListXJTEmerUndergroundPassage() {
        String sql="select * from T_EMER_UNDERGROUND_PASSAGE where is_delete=0";
        return getEntryList(sql,new XJTEmerUndergroundPassageRowMapper(),new Object[]{});
    }

    @Override
    public List<XJTEmerConcaveInterChange> getListXJTEmerConcaveInterChange() {
        String sql="select * from T_EMER_CONCAVE_INTERCHANGE where is_delete=0";
        return getEntryList(sql,new XJTEmerConcaveInterChangeRowMapper(),new Object[]{});
    }

    @Override
    public List<String> getListEmergencyResourceXJTRescueTeamType() {
        String sql="select task_type from T_RESCUE_TEAM group by task_type";
        return queryStringArray(sql,new Object[]{});
    }


    @Override
    public Integer getToday() {
        String sql="select count(id) from V_EVENT where TO_DAYS(event_time)=TO_DAYS(NOW())";
        return  queryInteger(sql,new Object[]{});
    }

    @Override
    public Integer getNowMonth() {
        String sql="select count(id) from V_EVENT where DATE_FORMAT(event_time,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m');";
        return  queryInteger(sql,new Object[]{});
    }

    @Override
    public List<XJTRescueTeam> getListXJTRescueTeamByType(String type) {
        String sql="select * from T_RESCUE_TEAM where is_delete=0 and task_type like ?";
        return getEntryList(sql,new XJTRescueTeamRowMapper(),new Object[]{"%"+type+"%"});
    }

    /**
     * 根据多个类型匹配列表
     * @param types
     * @return
     */
    @Override
    public List<XJTRescueTeam> getListXJTRescueTeamByTypes(String types) {
        String sql="select * from T_RESCUE_TEAM where is_delete=0 and task_type regexp ?";
        return getEntryList(sql,new XJTRescueTeamRowMapper(),new Object[]{types});
    }
}
